package com.shiyi.service.pdf.impl;

import cn.hutool.core.io.resource.ResourceUtil;
import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.shiyi.config.PageNumberEvent;
import com.shiyi.service.pdf.OpenPdfService;
import com.shiyi.utils.ConvertToChines;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;

@Service
@Slf4j
public class OpenPdfServiceImpl implements OpenPdfService {
    private static BaseFont SIMSUN;
//    private static Font SIMSUN_Text_10;
//    private static Font SIMSUN_Text_10f;

    static {
        try {
            URL resourceStream = ResourceUtil.getResource("SimSun.ttc");
              SIMSUN = BaseFont.createFont(resourceStream.toString() + ",1", BaseFont.IDENTITY_H, true);
//            SIMSUN_Text_10 = new Font(SIMSUN, 10);
//            SIMSUN_Text_10f = new Font(SIMSUN, 10f);

        } catch (Exception e) {

         }
    }

    @Override
    public void openPdf(String fileName) {
        // TODO 自动生成的方法存根
        String pdfFileName = fileName + ".pdf";
        Document document = new Document(PageSize.A4);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFileName);

            PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
            writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
            writer.setPageEvent(new PageNumberEvent());


            // ??
            document.open();

            // 创建第一页，目录
            document.newPage();

            Paragraph title = new Paragraph(fileName, new Font(SIMSUN, 16, Font.BOLD, Color.black));
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            //添加空行
            document.add(Chunk.NEWLINE);

            // 设置目录方法
            creteCatalog(document, title);

            document.newPage();

            // 添加内容
            for (int i = 0; i < 10; i++) {
                String no = ConvertToChines.convertToChinese(i);

                Anchor catalogAnchor = new Anchor("正文第" + no + "章---" + i, new Font(SIMSUN, 20, Font.BOLD, Color.black));
//                catalogAnchor.setReference("#" + i);
//                catalogAnchor.setName(i+"");
                catalogAnchor.setName(i+"");
                document.add(catalogAnchor);

                document.add(Chunk.NEWLINE);

                for (int j = 0; j < 10; j++) {
                    Anchor catalogChildrenAnchor = new Anchor("      **第" + no + "段" + j, new Font(SIMSUN, 14, Font.BOLD, Color.black));
//                    catalogChildrenAnchor.setReference("#" + "children" + j);
                    catalogChildrenAnchor.setName( "children" + j);
                    document.add(catalogChildrenAnchor);
                    document.add(Chunk.NEWLINE);
//                    Chunk chunk = new Chunk("摘要：" + j, new Font(SIMSUN, 12, Font.BOLD, Color.black));
//                    Chunk chunk_content = new Chunk("ssssssssssssssssdsdsdsdf" + j, new Font(SIMSUN, 12, Font.BOLD, Color.black));
//                    Paragraph paragraph = new Paragraph();
//                    paragraph.add(chunk);
//                    paragraph.add(chunk_content);
//                    // 设置行间距
//                    paragraph.setSpacingBefore(10);
//                    // 设置行间距
//                    paragraph.setLeading(20);
//                    document.add(paragraph);
                    addParagraph(document, j,"摘要：","dsssssdadfasfasdffddf");
                    addParagraph(document, j,"建议：","dsssssdadfasfasdffddf");


                }
            }


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            document.close();
        }


    }

    private void addParagraph(Document document, int j,String typeChineseContent,String content) {
        Chunk chunk_jianyi = new Chunk(typeChineseContent + j, new Font(SIMSUN, 12, Font.BOLD, Color.black));
        Chunk chunk_jianyi_content = new Chunk(j+"、"+content , new Font(SIMSUN, 12, Font.BOLD, Color.black));
        Paragraph paragraph_jianyi = new Paragraph();
        paragraph_jianyi.add(chunk_jianyi);
        paragraph_jianyi.add(chunk_jianyi_content);
        // 设置行间距
        paragraph_jianyi.setSpacingBefore(10);
        // 设置行间距
        paragraph_jianyi.setLeading(20);
        document.add(paragraph_jianyi);
    }

    /**
     * 设置目录
     * @param document
     * @param title
     */
    private void creteCatalog(Document document, Paragraph title) {
        Paragraph catalog = new Paragraph("目录", new Font(SIMSUN, 16, Font.BOLD, Color.black));
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(catalog);

        document.add(Chunk.NEWLINE);

        // 添加目录内容
        for (int i = 0; i < 10; i++) {
            String no = ConvertToChines.convertToChinese(i);

//                Paragraph catalogContent = new Paragraph("第" + no + "章", new Font(SIMSUN, 14, Font.BOLD, Color.black));
            Anchor catalogAnchor = new Anchor("目录-第" + no + "章1---" + i, new Font(SIMSUN, 10, Font.BOLD, Color.black));
            catalogAnchor.setReference("#" + i);
            document.add(catalogAnchor);

            document.add(Chunk.NEWLINE);

            for (int j = 0; j < 10; j++) {
                Anchor catalogChildrenAnchor = new Anchor("      目录第" + no + "段2" + j, new Font(SIMSUN, 10, Font.BOLD, Color.black));
                catalogChildrenAnchor.setReference("#" + "children" + j);
                document.add(catalogChildrenAnchor);

                document.add(Chunk.NEWLINE);
            }


        }

    }

//    private void creteCatalog(Document document, Paragraph title) {
//        Paragraph catalog = new Paragraph("目录", new Font(SIMSUN, 16, Font.BOLD, Color.black));
//        title.setAlignment(Element.ALIGN_CENTER);
//        document.add(catalog);
//
//        document.add(Chunk.NEWLINE);
//
//        // 添加目录内容
//        for (int i = 0; i < 10; i++) {
//            String no = convertToChines(i);
//
////                Paragraph catalogContent = new Paragraph("第" + no + "章", new Font(SIMSUN, 14, Font.BOLD, Color.black));
//            Anchor catalogAnchor = new Anchor("第" + no + "章---"+ i, new Font(SIMSUN, 10, Font.BOLD, Color.black));
//            catalogAnchor.setReference("#"+ i);
//            document.add(catalogAnchor);
//
//            document.add(Chunk.NEWLINE);
//
//            for (int j = 0; j < 10; j++) {
//                Anchor catalogChildrenAnchor = new Anchor("第" + no + "段"+ j, new Font(SIMSUN, 10, Font.BOLD, Color.black));
//                catalogChildrenAnchor.setReference("#"+"children"+ i);
//                document.add(catalogChildrenAnchor);
//
//                document.add(Chunk.NEWLINE);
//            }
//
//        }
//    }

//    private String  convertToChines(int i) {
//                String format = NumberChineseFormatter.format(i, false);
////        String format = "dddddddd";
//
//        return format;
//    }


}
